package com.yikaiye.android.yikaiye.ui.adapter;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.databinding.ItemCompanyIntroBinding;
import com.yikaiye.android.yikaiye.model.company.CompanyIntroModel;
import com.yikaiye.android.yikaiye.utils.UiUtils;

import java.util.List;

/**
 * 营业执照信息
 */
public class CompanyIntroAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context context;
    private final List<CompanyIntroModel> data;

    public CompanyIntroAdapter(Context context, List<CompanyIntroModel> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemCompanyIntroBinding itemCompanyIntroBinding = ItemCompanyIntroBinding.inflate(LayoutInflater.from(context), parent, false);
        return new MyViewHolder(itemCompanyIntroBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            CompanyIntroModel model = data.get(position);
            if (model != null) {
                myViewHolder.binding.setCompanyIntroModel(model);

                myViewHolder.binding.creditNoTv.setText(TextUtils.isEmpty(model.getCreditNo()) || model.getCreditNo().length() > 18 ? context.getString(R.string.no_data2) : model.getCreditNo());
                myViewHolder.binding.tvManagementStatus.setText(UiUtils.getManagementStatus(model.getNewStatusCode()));
                if (!TextUtils.isEmpty(model.getActualCapi())) {
                    myViewHolder.binding.tvReallyCapital.setText(context.getString(R.string.really_capital, model.getActualCapi()));
                }
                if (null != model.getIndustryCodeList() && !model.getIndustryCodeList().isEmpty()) {
                    StringBuilder stringBuilder = new StringBuilder();
                    for (String s : model.getIndustryCodeList()) {
                        stringBuilder.append(s).append(",");
                    }
                    myViewHolder.binding.tvIndustry.setText(stringBuilder.substring(0, stringBuilder.toString().length() - 1));
                }
                if (!TextUtils.isEmpty(model.getReportAddress())) {
                    if (!TextUtils.isEmpty(model.getReportYear())) {
                        myViewHolder.binding.tvReportAddress.setText(Html.fromHtml(context.getString(R.string.report_address, model.getReportAddress(), model.getReportYear()), Html.FROM_HTML_MODE_LEGACY));
                    } else {
                        myViewHolder.binding.tvReportAddress.setText(model.getReportAddress());
                    }
                }
                if (!TextUtils.isEmpty(model.getTermStart())) {
                    if (!TextUtils.isEmpty(model.getTermEnd())) {
                        myViewHolder.binding.tvTerm.setText(context.getString(R.string.term_range, model.getTermStart(), model.getTermEnd()));
                    } else {
                        myViewHolder.binding.tvTerm.setText(context.getString(R.string.no_term, model.getTermStart()));
                    }
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        ItemCompanyIntroBinding binding;

        public MyViewHolder(ItemCompanyIntroBinding itemCompanyIntroBinding) {
            super(itemCompanyIntroBinding.getRoot());
            binding = itemCompanyIntroBinding;
        }
    }
}
